package it.polimi.swKnights.SWIMv2.filters;

import it.polimi.swKnights.SWIMv2.ejb.authentication.LoginHandlerRemote;
import it.polimi.swKnights.SWIMv2.ejb.entityBeans.BannedUser;
import it.polimi.swKnights.SWIMv2.general.Errors;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet Filter implementation class BannedFilter
 */
public class BannedFilter extends SwimFilter implements Filter
{

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException
	{
		HttpServletRequest req = (HttpServletRequest) request;
		HttpSession session = req.getSession();
		String userName = (String) session.getAttribute("userName");
		
		LoginHandlerRemote loginBean = this.lookUp(LoginHandlerRemote.class);
		
		if(loginBean.hasPrivileges(userName, BannedUser.class))
		{
			HttpServletResponse resp = (HttpServletResponse) response;
			resp.sendRedirect("./userHome?error="+Errors.banneduser);	
		}
		else
		{
			chain.doFilter(request, response);
		}
	}

}
